package tabulatorCtrl;

import electionCtrl.Election;
//import electionCtrl.Candidate;

import java.io.File;
import xml.MockVotingXMLReader;
import xml.XMLFileReader;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import java.io.FileNotFoundException;
import javax.xml.stream.XMLStreamException;
import java.io.IOException;

public class TabulateRunner {
	public static void main(String[] args) {

	/*  
	Candidate[] ballot = new Candidate [9];

	ballot[0] = new Candidate("Dan Onorato & H. Scott Conklin", Candidate.Seat.GOVERNOR, "Democrat");
	ballot[1] = new Candidate("Tom Corbett & Jim Cawley", Candidate.Seat.GOVERNOR, "Republican");
	ballot[2] = new Candidate("Dan B. Frankel", Candidate.Seat.PENN_HOUSE, "Democrat");
	ballot[3] = new Candidate("Daniel Ulrich Wiseman", Candidate.Seat.PENN_HOUSE, "Republican");
	ballot[4] = new Candidate("Mike Doyle", Candidate.Seat.US_HOUSE, "Democrat");
	ballot[5] = new Candidate("Melissa Haluszczak", Candidate.Seat.US_HOUSE, "Republican");
	ballot[6] = new Candidate("Joe Sestak", Candidate.Seat.US_SENATE, "Democrat");
	ballot[7] = new Candidate("Pat Toomey", Candidate.Seat.US_SENATE, "Republican");
	ballot[8] = new Candidate(".", Candidate.Seat.GOVERNOR, "Grammar");
          Candidate[] votes1 = new Candidate [12];

          votes1[0] = new Candidate("Dan Onorato & H. Scott Conklin", Candidate.Seat.GOVERNOR, "Democrat");
          votes1[1] = new Candidate("Dan Onorato & H. Scott Conklin", Candidate.Seat.GOVERNOR, "Democrat");
          votes1[2] = new Candidate("Tom Corbett & Jim Cawley", Candidate.Seat.GOVERNOR, "Republican");
          votes1[3] = new Candidate("Tom Corbett & Jim Cawley", Candidate.Seat.GOVERNOR, "Republican");
          votes1[4] = new Candidate("Joe Sestak", Candidate.Seat.US_SENATE, "Democrat");
          votes1[5] = new Candidate("Joe Sestak", Candidate.Seat.US_SENATE, "Democrat");
          votes1[6] = new Candidate("Pat Toomey", Candidate.Seat.US_SENATE, "Republican");
          votes1[7] = new Candidate("Pat Toomey", Candidate.Seat.US_SENATE, "Republican");
          votes1[8] = new Candidate("Joe Sestak", Candidate.Seat.US_SENATE, "Democrat");
          votes1[9] = new Candidate("Joe Sestak", Candidate.Seat.US_SENATE, "Democrat");
          votes1[10] = new Candidate("Pat Toomey", Candidate.Seat.US_SENATE, "Republican");
          votes1[11] = new Candidate("Pat Toomey", Candidate.Seat.US_SENATE, "Republican");
	*/
		
		Tabulator t = new Tabulator();
		String outputLoc = "";

		if (args.length > 2) {//ballot location, output location, output location, files to tabulate
			try {
				MockVotingXMLReader b = new MockVotingXMLReader(args[0]);
				Election e = new Election(b);
				outputLoc = args[1];
				t = new Tabulator(e.getCandidates(),outputLoc);
				for (int i = 2; i < args.length; i++) {
					MockVotingXMLReader r = new MockVotingXMLReader(args[i]);
					t.addVotes(r);
				}
			} catch (FileNotFoundException e) {
				JOptionPane.showMessageDialog(null, "File not found"  + e.getMessage() + ". The application will now terminate.");
				System.exit(0);
			} catch (XMLStreamException e) {
				JOptionPane.showMessageDialog(null, "A .vot file was improperly formatted. The application will now terminate.");
				System.exit(0);
			} catch (IOException e) {
				JOptionPane.showMessageDialog(null, "Unable to read/write. The application will now terminate.");
				System.exit(0);
			}
			

		} else {
			try {
				JFileChooser chooser = new JFileChooser();
				chooser.setDialogType(JFileChooser.SAVE_DIALOG); 
				JOptionPane.showMessageDialog(null, "Select a location to save the tabulation output.");
				int rc = chooser.showSaveDialog(null);
				if (rc == JFileChooser.APPROVE_OPTION) {
					outputLoc = chooser.getSelectedFile().getAbsolutePath();
				}
				
				while(new File(outputLoc).exists()) {
					JOptionPane.showMessageDialog(null, "You selected a file that already exists. Please enter a new file name so as to ensure nothing is overwritten.");
					JOptionPane.showMessageDialog(null, "Select a location to save the tabulation output.");
					rc = chooser.showSaveDialog(null);
					if (rc == JFileChooser.APPROVE_OPTION) {
						outputLoc = chooser.getSelectedFile().getAbsolutePath();
					}
				}
	
				chooser.setDialogType(JFileChooser.OPEN_DIALOG);
				JOptionPane.showMessageDialog(null, "Select a ballot file.");
				chooser.showOpenDialog(null);
				String bltLoc = chooser.getSelectedFile().toString();
				String ext = bltLoc.substring((bltLoc.lastIndexOf('.')+1), bltLoc.length());
				while (!(ext.equals("blt"))) {
					JOptionPane.showMessageDialog(null, "You didn't select a ballot. Ballots are .blt files.");
					JOptionPane.showMessageDialog(null, "Select a ballot file.");
					chooser.showOpenDialog(null);
					bltLoc = chooser.getSelectedFile().toString();
					ext = bltLoc.substring((bltLoc.lastIndexOf('.')+1), bltLoc.length());
				}

				Election e = new Election(new MockVotingXMLReader(bltLoc));
				t = new Tabulator(e.getCandidates(),outputLoc);
	
				XMLFileReader r2 = new XMLFileReader();
				for (File f : r2.getFiles()) {
					MockVotingXMLReader r3 = new MockVotingXMLReader(f.getAbsoluteFile().toString());
					t.addVotes(r3);
				}
			} catch (FileNotFoundException e) {
				JOptionPane.showMessageDialog(null, "File not found: " + e.getMessage() + ". The application will now terminate.");
				System.exit(0);
			} catch (XMLStreamException e) {
				JOptionPane.showMessageDialog(null, "A .vot file was improperly formatted. The application will now terminate.");
				System.exit(0);
			} catch (IOException e) {
				JOptionPane.showMessageDialog(null, "Unable to read/write. The application will now terminate.");
				System.exit(0);
			}
		}

		if (t != null) t.tabulate();
		try {
			TabulatorGUI tgui = new TabulatorGUI(t.printReport());
			tgui.showWindow();
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "Couldn't open review screen to show final results. IO error.");
		}
				
		try {
			t.writeFile(); //writes and closes the output file.
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "Couldn't write to output file- may be corrupted or improperly formatted.");
		}
	}
}